package com.tools.controller;

import com.tools.common.beans.constant.ControllerConstant;
import com.tools.common.beans.vo.Result;
import com.tools.generator.entity.FieldTypeEntity;
import com.tools.generator.service.FieldTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * 字段类型管理
 * @author ShangJY
 */
@RestController
@RequestMapping(ControllerConstant.API_MAPPING_PREFIX)
public class FieldTypeController extends BaseController {

    @Autowired
    private FieldTypeService fieldTypeService;

    /**
     * 基础路径
     */
    private final String prefix = "generate/";

    @GetMapping(value = {"/{page}.html"})
    public ModelAndView allPage(@PathVariable("page") String page){
        return jumpPage(prefix + page);
    }

    @GetMapping(value = {"/mapping-edit"})
    public ModelAndView editPage(@RequestParam("id") Long id){
        FieldTypeEntity byId = fieldTypeService.getById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("fieldType",byId);
        modelAndView.setViewName(prefix + "mapping-edit");
        return modelAndView;
    }

    @GetMapping("page")
    public Result page(FieldTypeEntity query) {
        startPage(query.getPage(),query.getLimit());
        List<FieldTypeEntity> page = fieldTypeService.page(query);
        return getDataTable(page);
    }

    @GetMapping("{id}")
    public Result<FieldTypeEntity> get(@PathVariable("id") Long id) {
        FieldTypeEntity data = fieldTypeService.getById(id);
        return Result.OK(data);
    }

    @GetMapping("list")
    public Result<Set<String>> list() {
        Set<String> set = fieldTypeService.getList();
        return Result.OK(set);
    }

    @PostMapping("save")
    public Result<String> save(@RequestBody FieldTypeEntity entity) {
        fieldTypeService.save(entity);
        return Result.OK();
    }

    @PutMapping("update")
    public Result<String> update(@RequestBody FieldTypeEntity entity) {
        fieldTypeService.updateById(entity);
        return Result.OK();
    }

    @DeleteMapping("delete")
    public Result<String> delete(@RequestBody Long[] ids) {
        fieldTypeService.removeBatchByIds(Arrays.asList(ids));
        return Result.OK();
    }
}